import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from tkinter.filedialog import askdirectory
from tkinter import messagebox
import testForJVM as runJVM
import copyAndRenameFilesInJava as copyFiles
import os


class createWindow(ttk.Frame):
    # 构造函数
    def __init__(self):
        self.app = ttk.Window("文件复制重命名 —— By 半猫", "superhero")
        # 禁止手动缩放
        self.app.resizable(0, 0)
        super().__init__(self.app, padding=15)
        style = ttk.Style()

        self.resourcePath = ttk.StringVar()
        self.inOrder = ttk.StringVar()
        self.prefixStr = ttk.StringVar()
        self.prefixStr.set("Copy")
        self.suffixLen = ttk.StringVar()
        self.suffixLen.set("3")
        self.exportdir = ttk.StringVar()

        self.app.geometry("800x1100")
        self.app.position_center()

        self.pack(fill=BOTH, expand=YES)
        self.create_widget_elements()
        # self.printPath()
        self.app.mainloop()

    def create_widget_elements(self):
        # 页面域
        frame1 = ttk.Frame(self, padding=20)
        frame1.pack()

        # 分界栏
        label_frame = ttk.Labelframe(frame1, bootstyle="info", text="选择源文件夹", width=750, height=100)
        label_frame.pack()
        # 标签域
        # label1 = ttk.Label(label_frame, text=" ", width=750)
        # label1.pack(fill=X, padx=20)
        # print(help(label_frame))
        # 路径文本域
        file_entry = ttk.Entry(label_frame, textvariable=self.resourcePath, width=40)
        file_entry.pack(side=LEFT, fill=X, expand=YES, padx=(20, 15), pady=20)
        # 文件路径按钮
        dir_btn = ttk.Button(label_frame, text="浏览", command=self.open_resource_dir)
        dir_btn.pack(side=RIGHT, fill=X, padx=(5, 20), pady=10)

        # 分界栏
        label_frame2 = ttk.Labelframe(master=frame1, bootstyle="info", text="选择配置", width=750, height=100)
        label_frame2.pack(pady=30)

        col1 = ttk.Frame(label_frame2, padding=5)
        col1.grid(row=0, column=0, sticky=NSEW)
        # 标签域
        label2 = ttk.Label(col1, text="排序方式", width=10)
        label2.pack(fill=X, padx=20)
        # 下拉框
        combobox = ttk.Combobox(master=col1, textvariable=self.inOrder,
                                values=['1-时间升序', '2-时间降序', '3-文件名升序', '4-文件名降序'], state="readonly")
        combobox.current(0)
        combobox.pack(fill=X, padx=20, pady=20)

        col2 = ttk.Frame(label_frame2, padding=5)
        col2.grid(row=0, column=1, sticky=NSEW)
        # 标签域
        label2 = ttk.Label(col2, text="文件前缀", width=10)
        label2.pack(fill=X, padx=20)
        # 下拉框
        entry2 = ttk.Entry(master=col2, textvariable=self.prefixStr)
        entry2.pack(fill=X, padx=20, pady=20)

        # 标签域
        label3 = ttk.Label(col2, text="后缀长度", width=10)
        label3.pack(fill=X, padx=20)
        # 下拉框
        entry3 = ttk.Entry(master=col2, textvariable=self.suffixLen)
        entry3.pack(fill=X, padx=20, pady=20)

        col3 = ttk.Frame(label_frame2, padding=5)
        col3.grid(row=1, column=0, columnspan=2, sticky=NSEW)
        # 标签域
        label4 = ttk.Label(col3, text="输出路径", width=10)
        label4.pack(fill=X, padx=20)
        # 输出路径文本域
        file_entry2 = ttk.Entry(col3, textvariable=self.exportdir, width=4)
        file_entry2.pack(side=LEFT, fill=X, expand=YES, padx=(20, 15), pady=20)
        # 文件路径按钮
        dir_btn2 = ttk.Button(col3, text="浏览", command=self.open_export_dir)
        dir_btn2.pack(side=RIGHT, fill=X, padx=(5, 20), pady=10)

        col4 = ttk.Frame(label_frame2, padding=5)
        col4.grid(row=2, column=0, columnspan=2, sticky=NSEW)
        # dir_btn = ttk.Button(col4, text="测试", command=self.testJavaPrint)
        dir_btn = ttk.Button(col4, text="开始复制文件", command=self.renameFilesInJava)
        dir_btn.pack(side=BOTTOM, fill=BOTH, padx=(5, 20), pady=10)
        # self.resultText.insert("1.0", "aaa")

        # 分界栏
        label_frame3 = ttk.Labelframe(master=frame1, bootstyle="info", text="输出结果", width=750, height=100)
        label_frame3.pack(pady=30)

        self.resultText = ttk.Text(master=label_frame3, width=40, state="disabled", height=5)
        self.resultText.pack(padx=20, pady=20)

    def open_resource_dir(self):
        path = askdirectory()
        # print(self.resourcePath)
        self.resourcePath.set(path)

    def open_export_dir(self):
        path = askdirectory()
        self.exportdir.set(path)

    def printPath(self):
        print("打印路径：", self.inOrder.get())
        self.app.after(5000, self.printPath)

    def testJavaPrint(self):
        # 执行Java代码
        result = runJVM.run(self.inOrder)
        # 回显执行结果
        self.resultText.config(state="normal")
        self.resultText.delete("1.0", END)
        self.resultText.insert(END, result)
        self.resultText.config(state="disabled")

    def renameFilesInJava(self):
        # 执行Java代码
        result = copyFiles.runJava(self.resourcePath.get(),
                                   self.inOrder.get(),
                                   self.prefixStr.get(),
                                   self.suffixLen.get(),
                                   self.exportdir.get())
        # 回显执行结果
        self.resultText.config(state="normal")
        self.resultText.delete("1.0", END)
        self.resultText.insert(END, result.get("msg"))
        self.resultText.config(state="disabled")
        if result.get("error") is None:
            messagebox.showinfo("提示信息", "复制成功！")
            openFileDir = self.exportdir.get()
            if openFileDir is None or openFileDir == "":
                openFileDir = self.resourcePath.get()
            print("打开目标目录", openFileDir, sep=": ")
            os.startfile(openFileDir)


def run():
    return createWindow()
    # print(instance.filename.get())


if __name__ == "__main__":
    instance = run()
